package com.googlecode.garbagecan.perf4jstudy;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.log4j.Logger;
import org.perf4j.LogParser;
import org.perf4j.StopWatch;
import org.perf4j.helpers.GroupedTimingStatisticsTextFormatter;
import org.perf4j.log4j.Log4JStopWatch;

// java -Dcom.sun.management.jmxremote -cp target/classes:target/perf4jstudy/WEB-INF/lib/perf4j-0.9.9.jar:target/perf4jstudy/WEB-INF/lib/log4j-1.2.14.jar com/googlecode/garbagecan/perf4jstudy/Test3
public class Test3 {
	public static void main(String[] args) throws Exception {
		test1();
		test2();
	}
	
	private static void test1() throws Exception {
		Logger rootLogger = Logger.getRootLogger();
		for (int i = 0; i < 10; i++) {
			// By default the Log4JStopWatch uses the Logger named
			// org.perf4j.TimingLogger
			StopWatch stopWatch = new Log4JStopWatch();

			// for demo purposes just sleep
			Thread.sleep((long) (Math.random() * 1000L));

			rootLogger.info("Normal logging messages only go to the console");

			// Calling lap() stops timing for the previous block, sends the
			// message to the log4j Logger, and starts timing the next block.
			stopWatch.lap("firstBlock");

			Thread.sleep((long) (Math.random() * 2000L));

			stopWatch.stop("secondBlock");
		}
	}

	private static void test2() throws Exception {
		Reader reader = new InputStreamReader(new FileInputStream(new File("perf4j.log").getAbsolutePath()));
		LogParser logParser = new LogParser(reader, System.out, null, 60 * 1000, true, new GroupedTimingStatisticsTextFormatter());
		logParser.parseLog();
	}
}
